var IaaS = IaaS || {};
/*IaaS.ui = {
	progressBar: function(options){
		options = $.extend({
			title: '8core',
			bgcolor: '#888',
			max: 100,
			min: 0,
			value1: 0,
			value2: 0
		},options);
		
		var bar;
		return bar;
	},
	slider: function(){},
	toggleButton: function(options){
		var default_state = options.state !== 0 ? 1 : 0;
		var callback = options.callback || function(){};
		var btn = $('<img />').appendTo(options.container);
		var change = function(s){
			btn.attr({
				'src': '../images/' + ['close.png','open.png'][s],
				'title': ['打开','关闭'][s]
			}).data('state',s);
		}
		btn.bind({
			'click': function(event){
				var ret = callback.call(this,$(this).data('state'));
				if(ret !== false){
					var s = /open/.test(this.src) ? 0 : 1;
					change(s);
				}
			},
			'setState': function(event,s){
				change(s);
			}
		});
		btn.trigger('setState',[default_state]);
		return btn;
	}
};*/
IaaS.Layout = {
	initTopbar: function(){
		var $top = $('#iaas_layout_top');
		var logoTrigger = $('.logo,.arrowdown',$top);
		var mainMenu = $('.menu',$top);
		var hideTimer;
		var menuMask = $('<div class="menu-mask" />').appendTo(document.body);
		logoTrigger.click(function(event){
			if(mainMenu.is(':visible')){
				mainMenu.hide();
				menuMask.hide();
			}else{
				mainMenu.show();
				menuMask.height($(document).height()-62).show();
			}
		});
		mainMenu.hover(function(){
			clearTimeout(hideTimer);
		},function(){
			hideTimer = setTimeout(function(){
				mainMenu.hide();
				menuMask.hide();
			},300);
		});
		$('.user-menu-item').each(function(i,el){
			if($(el).children('.navbar').size() > 0){
				$(el).hover(function(){
					$('.navbar',this).stop(true,true).fadeIn('fast');
				},function(){
					$('.navbar',this).stop(true,true).fadeOut('fast');
				});
			}
		});
	},
	bindSidebar: function(){
		var $side = $('#iaas_layout_sidebar');
		var $con = $('#iaas_layout_content');
		var $top = $('#iaas_layout_top');
		var $foot = $('#footer');
		/*var resizeTimer;
		$(window).resize(function(){
			clearTimeout(resizeTimer);
			resizeTimer = setTimeout(function(){
				$side.css('height',$con.height());
			},100);
		});*/
		setInterval(function(){
			$side.css('height',$con.height());
		},300);
		$('.unfold a',$side).click(function(event){
			return false;
		});
		$('.unfold img',$side).click(function(){
			var src = this.src;
			if(src.indexOf('unfold') >= 0){
				this.src = src.replace('unfold','contract');
				$side.removeClass('sidebar_close').addClass('sidebar_open');
			}else{
				this.src = src.replace('contract','unfold');
				$side.removeClass('sidebar_open').addClass('sidebar_close');
			}
		}).hover(function(){
			this.src = this.src.replace('.gif','_h.gif');
		},function(){
			this.src = this.src.replace('_h.gif','.gif');
		});
	},
	init: function(){
		IaaS.Layout.initTopbar();
		IaaS.Layout.bindSidebar();
	}
};
IaaS.TPManage = {
	dialogHTML: '<div class="white_content"><div class="close1" node-type="close"><a href = "javascript:void(0)"><img src="../images/uclose.png" /></a></div><div class="tp_dialog_content">{CONTENT}</div></div>',
	showDialog: function(options){
		var html = IaaS.TPManage.dialogHTML;
		var content = '';
		if(options.url){
			content = '<iframe src="'+ options.url +'" width="530" scrolling="no" height="285" frameborder="0" allowtransparency="true"></iframe>';
		}
		html = html.replace('{CONTENT}',content);
		$.dialog({
			skin:false,
			content: html,
			afterHide: function(){
				try{
					$('iframe',this.boxy).attr('src','about:blank');
				}catch(e){}
			}
		});
	},
	showComInfo: function(url){
		IaaS.TPManage.showDialog({url: url});
	},
	showVMInfo: function(url){
		IaaS.TPManage.showDialog({url: url});
	}
}
function deleteConfirm(text,callback){
	
	var delete_tip = '<div class="Popup"><div class="Popupheader">\
		<a href="javascript:void(0)" onclick="$.dialog.close()"><img src="../images/Popupclose01.png" width="28" height="28" border="0" /></a></div>\
		<div class="Popup_A">\
		<div class="Popup_A_1 p30">\
		<p class="delete_2"><img src="../images/ask.png" width="32" height="32" />'+ text +'</p>\
		<div class="btnblock">\
				<input name="Input" type="button" value="确 定" class="btngreen01"/><input onclick="$.dialog.close()" name="Input" type="button" value="取 消" class="btngray01"/>\
		</div></div></div>\
		<div class="Popupfoot"><img src="../images/Popupbg01_footer.png" width="550" height="15" /></div>\
		</div>';


	var delete_pop = $.dialog({
		skin: false,
		content: delete_tip
	});
	var confirmbtn = delete_pop.boxy.find(':button:eq(0)');
	confirmbtn.click(function(){
		$.dialog.close();
		callback();
	});
}
function createUsePie(options){
	var chartId = options.container + 'Id';
	var width = options.width || 88;
	var height = options.height || 88;
	var use_percent = options.data[0];
	var left_percent = options.data[1];
	var chart_options = {
		"chart": {
			"animation":"0",
			"showvalues": "0",
			"showlabels": "0",
			"showlegend": "0",
			"chartLeftMargin": "0",
			"chartRightMargin": "0",
			"chartTopMargin": "0",
			"chartBottomMargin": "0",
			"enableRotation": "0", //旋转
			"pieRadius": width/2-1, //pie半径
			"slicingDistance": "0", //点击移动距离
			"bgAlpha": "0",//透明度
			"showBorder": "0" ,//边框,
			"use3DLighting": '0',
			"showShadow": "0",
			"showToolTip": options.showtip===0?0:1
		},
		"data": [
			{"value": use_percent,"color": "68c211","label":"已用"},
			{"value": left_percent,"color": "bff589","label":"可用"}
		]
	};
	var chartobj = new FusionCharts("Doughnut2D",chartId,width,height);
	chartobj.setJSONData(chart_options);
	chartobj.render(options.container);
	if(options.title){
		$('<span class="use_pie_title" />').html(options.title).appendTo('#'+options.container);
	}
}

function initProgressBar(elem,start,disable){
	start = start || 0;
	var $bar = $(elem);
	$bar.noUiSlider({
		range:[0,100],
		start:[start],
		step: 1,
		handles:1,
		serialization:{to:['range']},
		slide: function(){
			$('.slide_bar',this).css('width',$(this).val()+'%');
			$('.slide_value',this).html($(this).val()+'%');
		}
	});
	$('.slide_bar',$bar).css('width',start+'%');
	$('<span class="slide_value"></span>').appendTo($bar.find('.slide_handle')).html(start+'%');
	if(disable){
		$bar.addClass('disabled');
	}
}

function colorPicker(elem,defaultColor){
	if(defaultColor){
		elem.value = defaultColor;
	}
	new jscolor.color(elem,{pickerClosable: true});
}
/*
	type: info,success,error,warning,help
	text: string
*/
var tipHTML = '<div id="msgBox" class="messageBox">\
	<p class="messageBox-text"></p>\
	<p><a class="alert-close" href="javascript:void(0);">Close</a></p>\
</div>';
var tipCss = 'info success error warning help';
var tipTimer;
function customTip(type,text){
	var msgbox = $('#msgBox');
	if(msgbox.length == 0){
		msgbox = $(tipHTML).appendTo(document.body);
		msgbox.find('.alert-close').click(function(){
			clearTimeout(tipTimer);
			msgbox.fadeOut();
		});
	}
	msgbox.removeClass(tipCss).addClass(type).find(".messageBox-text").html(text);
	msgbox.css({
		"width": "80%",
		"position": "absolute",
		"visibility": "hidden",
		"display": "block"
	});
	var boxHeight = msgbox.outerHeight(true);
	var boxWidth = msgbox.outerWidth(true);
	var scrollTop = $(window).scrollTop();
	msgbox.css({
		"visibility": "visible",
		"opacity": 0,
		"top": scrollTop - boxHeight,
		"left": $(window).width()/2 - boxWidth/2
	}).animate({top: scrollTop,opacity:.9});
	clearTimeout(tipTimer);
	tipTimer = setTimeout(function(){
		msgbox.fadeOut();
	},2500);
}
//Popup1
var Popup1HTML = '<div class="Popup">\
	<div class="Popupheader"><a node-type="close" href="#"><img src="../images/Popupclose01.png"/></a></div>\
	<div class="Popup_A"><div class="Popup_A_1"></div></div>\
	<div class="Popupfoot"><img src="../images/Popupbg01_footer.png"/></div>\
</div>';

$(document).ready(function(){
	IaaS.Layout.init();
});

// Requires jQuery!
/*jQuery.ajax({
    url: "http://jira.bjcathay.com/s/zh_CN7mn04l-418945332/849/3/1.2.9/_/download/batch/com.atlassian.jira.collector.plugin.jira-issue-collector-plugin:issuecollector-embededjs/com.atlassian.jira.collector.plugin.jira-issue-collector-plugin:issuecollector-embededjs.js?collectorId=2aa4e6d7",
    type: "get",
    cache: true,
    dataType: "script"
});*/
